package com.umeox.watch.moto.launcher.ui.widget;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeox.watch.moto.launcher.R;
import com.umeox.watch.moto.launcher.event.WatchFaceChangeEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Locale;

public class CartoonsNumberClockView extends RelativeLayout {

    private static final int BATTERY_FULL = 100;
    private final Typeface mTfJosefinSansBold;
    private Typeface mTfJosefinSansLight;
    private Typeface mTfLatoBold;
    private TextView mHourTv;
    private TextView mMinuteTv;
    private TextView mDateTv;
    private Context mContext;
    private TextView mAmPmTv;
    public final SimpleDateFormat DATE_FORMAT_24 = new SimpleDateFormat("EEE d. MMM-HH-mm-a", Locale.getDefault());
    public final SimpleDateFormat DATE_FORMAT_12 = new SimpleDateFormat("EEE d. MMM-hh-mm-a", Locale.getDefault());
    public final SimpleDateFormat DATE_FORMAT_CN = new SimpleDateFormat("EEE MM月dd日-hh-mm-a", Locale.getDefault());
    public final SimpleDateFormat DATE_FORMAT_CN_24 = new SimpleDateFormat("EEE MM月dd日-HH-mm-a", Locale.getDefault());



    private View mView1;
    private View mView2;

    public CartoonsNumberClockView(Context context) {
        this(context, null);
    }

    public CartoonsNumberClockView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CartoonsNumberClockView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        AssetManager mgr = context.getAssets();
        mTfJosefinSansBold = Typeface.createFromAsset(mgr, "font/JosefinSans_Bold.ttf");
        mTfJosefinSansLight = Typeface.createFromAsset(mgr, "font/JosefinSans_Light.ttf");
        mTfLatoBold = Typeface.createFromAsset(mgr, "font/Lato_Bold.ttf");
        initView();
    }

    private void initView() {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.view_cartioons_clock, this);
        mHourTv = view.findViewById(R.id.tv_hour);
        mView1 = view.findViewById(R.id.view_dots_1);
        mView2 = view.findViewById(R.id.view_dots_2);
        mHourTv = view.findViewById(R.id.tv_hour);
        mDateTv = view.findViewById(R.id.tv_date);

        mMinuteTv = view.findViewById(R.id.tv_minute);
        mMinuteTv.setTypeface(mTfJosefinSansLight);
        mHourTv.setTypeface(mTfJosefinSansBold);
        mDateTv.setTypeface(mTfLatoBold);
        mAmPmTv = view.findViewById(R.id.tv_am_pm);
        updateDateTime();
        //TODO
        initWallpaper(/*DataProvider.getWallpaper()*/0);
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        EventBus.getDefault().register(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        mContext.registerReceiver(mBroadcastReceiver, filter);
        updateDateTime();
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onWatchFaceChangeEvent(WatchFaceChangeEvent event) {
        final int position = event.mPosition;
        initWallpaper(position);

    }

    private void initWallpaper(int position) {

        final int[] colors = mContext.getResources().getIntArray(R.array.date_colors);
        final int color = colors[position];
        GradientDrawable dotsDrawable = new GradientDrawable();
        dotsDrawable.setColor(color);
        dotsDrawable.setSize(6, 6);
        dotsDrawable.setCornerRadius(3);

        mView1.setBackground(dotsDrawable);
        mView2.setBackground(dotsDrawable);
        mDateTv.setTextColor(color);
        mHourTv.setTextColor(color);
        mMinuteTv.setTextColor(color);
        mAmPmTv.setTextColor(color);
    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        EventBus.getDefault().unregister(this);
        mContext.unregisterReceiver(mBroadcastReceiver);
    }


    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateDateTime();

        }
    };


    /**
     * 更新日期\时间
     */
    private void updateDateTime() {
        Log.d("clock_view", "updateDateTime: ");
        final boolean hourFormat_24 = DateFormat.is24HourFormat(mContext);
        SimpleDateFormat dateFormat;
        final String language = Locale.getDefault().getLanguage();
        final boolean sv = language.equals("sv");
        if (language.equals("zh") || language.equals("ja")) {
            dateFormat = hourFormat_24 ? DATE_FORMAT_CN_24 : DATE_FORMAT_CN;
        } else {
            dateFormat = hourFormat_24 ? DATE_FORMAT_24 : DATE_FORMAT_12;
        }
        mAmPmTv.setVisibility(hourFormat_24 ? INVISIBLE : VISIBLE);
        final long millis = System.currentTimeMillis();
        final String format = dateFormat.format(millis);
        final String[] dates = format.split("-");
        String date = dates[0];
        if (sv) {
            if (date.endsWith("mar")) {
                date += "s";
            } else if (date.endsWith("jun") || date.endsWith("jul")) {
                date += "i";
            } else if (date.endsWith("sep")) {
                date += "t";
            } else {
                date += ".";
            }
        }

        mDateTv.setText(date);
        mHourTv.setText(dates[1]);
        mMinuteTv.setText(dates[2]);
        mAmPmTv.setText(dates[3]);
    }


}
